AP CSA – Class Notes

1.12 Objects: Instances of Classes

1. Overview

In Java, classes and objects form the foundation of object-oriented programming (OOP). A class is a blueprint that describes how objects of that type behave and what data they store. An object is a real instance created from that blueprint. You can create many different objects from the same class, and each object stores its own unique data.

2. What Is a Class?

A class defines two major parts:

A. Instance Variables (State)

These are the pieces of data every object of the class will store.

private String name;
private int age;

B. Methods (Behavior)

These are the actions the object can perform.

public void setAge(int a) { age = a; }
public int getAge() { return age; }

A class itself is NOT an object. It is only the design.

3. What Is an Object?

An object is created from a class using the new keyword.

Dog d1 = new Dog();
Dog d2 = new Dog();

Each of these statements creates a new Dog object in memory. The variable (e.g., d1) is a reference variable that points to the object.

4. Objects Hold Different Values

Even though objects come from the same class, they can hold different data.

Student s1 = new Student("Aiden", 95);
Student s2 = new Student("Jolie", 87);

s1 stores Aiden's data, while s2 stores Jolie's data. This is why objects represent individual items with their own state.

5. Dot Notation

You access an object's behavior using the dot (.) operator:

s1.getGrade();
s2.setGrade(100);

6. Reference Variables

Object variables do not store the object directly—they store a reference (memory address) pointing to the object.

Student s1 = new Student("Ben", 90);

Here:

7. Real-World Analogy

Class: Car Blueprint
Objects: Actual Cars (Toyota, Honda, Tesla)

Each car shares the same design but has different colors, mileage, and owners. This is exactly how class instances differ in Java.

8. Key Terms

TermDefinition
ClassBlueprint defining attributes (state) and methods (behavior)
ObjectA real instance created from a class
Instance VariableData stored inside each object
MethodBehavior an object can perform
InstantiationCreating an object using new
StateThe values stored in an object
BehaviorActions the object performs
Reference VariableStores the memory address of an object

9. Example Class & Objects

Class Definition

public class Dog {
  private String name;
  private int age;

  public Dog(String n, int a) {
    name = n;
    age = a;
  }

  public void bark() {
    System.out.println(name + " says Woof!");
  }
}

Creating Objects

Dog d1 = new Dog("Max", 4);
Dog d2 = new Dog("Bella", 2);

Using Objects

d1.bark(); // Max says Woof!
d2.bark(); // Bella says Woof!

10. Importance in AP CSA

Understanding objects helps students succeed in:

Objects are the building blocks of Java programs.